package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationVo;
import cn.ffcs.uec.staff.dao.UecEncDao;
import cn.ffcs.uec.staff.model.UecEnc;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecEncDao")
@Transactional
public class UecEncDaoImpl extends BaseDaoImpl implements UecEncDao {

	@Override
	public PageInfo queryPageInfoByUecEnc(UecEnc uecEnc, int current,
			int pageSize) {
		if (null != uecEnc) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecEnc
					.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}

			if (!StrUtil.isNullOrEmpty(uecEnc.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecEnc.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecEnc.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecEnc.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecEnc.class);
		}
		return null;
	}

	@Override
	public UecEnc findUecEncById(UecEnc uecEnc) {
		if (null != uecEnc) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecEnc.getEncId())) {
				sb.append(" AND T.ENC_ID = ? ");
				params.add(uecEnc.getEncId());
			}
			List<UecEnc> uecEncs = this.jdbcFindList(sb.toString(), params,
					UecEnc.class);
			if (uecEncs.size() > 0) {
				return uecEncs.get(0);
			} else {
				return null;
			}
		}
		return uecEnc;
	}

	@Override
	public PageInfo queryPageInfoByUecEncOnApproval(UecEnc uecEnc, int current,
			int pageSize) {
		if (null != uecEnc) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecEnc
					.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}

			if (!StrUtil.isNullOrEmpty(uecEnc.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecEnc.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecEnc.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecEnc.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecEnc.class);
		}
		return null;
	}
/**
 * 近三年荣誉情况
 */
	@Override
	public List<UecEnc> findUecEncByOutStaffId(UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
				sb.append(" AND T.encourdate <>'无' and T.encourdate is not null and TO_DATE(T.encourdate,'yyyy-mm-dd') > TO_DATE('2016-01-01','yyyy-mm-dd') and TO_DATE(T.encourdate,'yyyy-mm-dd') < TO_DATE('2020-01-31','yyyy-mm-dd') ");
				sb.append(" order by to_date(T.encourdate,'yyyy-mm-dd') ");		
			return this.jdbcFindList(sb.toString(), params, UecEnc.class);
		}
		return null;
	}

	@Override
	public List<UecEnc> findUecEncByOutAndStaffId(UecOutStaffInfomationVo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId()) && uecOutStaffInfo.getOutStaffId()>0) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
				sb.append(" AND T.encourdate <>'无' and T.encourdate is not null and TO_DATE(T.encourdate,'yyyy-mm-dd') > TO_DATE('2015-01-01','yyyy-mm-dd') and TO_DATE(T.encourdate,'yyyy-mm-dd') < TO_DATE('2018-12-31','yyyy-mm-dd') ");
				sb.append(" order by to_date(T.encourdate,'yyyy-mm-dd') ");	
			}
			/*if(!StrUtil.isNullOrEmpty(uecOutStaffInfo.getStaffId())) {
				sb.append(" AND T.NM_STAFF_TYPE IS NULL ");
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecOutStaffInfo.getStaffId());
				sb.append(" AND T.encourdate <>'无' and T.encourdate is not null and TO_DATE(T.encourdate,'yyyy-mm-dd') > TO_DATE('2015-01-01','yyyy-mm-dd') and TO_DATE(T.encourdate,'yyyy-mm-dd') < TO_DATE('2018-12-31','yyyy-mm-dd') ");
				sb.append(" order by to_date(T.encourdate,'yyyy-mm-dd') ");	
			}*/
			return this.jdbcFindList(sb.toString(), params, UecEnc.class);
		}
		return null;
	}

	@Override
	public List<UecEnc> findUecEncByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ENC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				//sb.append(" AND T.encourdate <>'无' and T.encourdate is not null and TO_DATE(T.encourdate,'yyyy-mm-dd') > TO_DATE('2015-01-01','yyyy-mm-dd') and TO_DATE(T.encourdate,'yyyy-mm-dd') < TO_DATE('2018-12-31','yyyy-mm-dd') ");
				sb.append(" order by to_date(T.encourdate,'yyyy-mm-dd') ");	
			}
			return this.jdbcFindList(sb.toString(), params, UecEnc.class);
		}
		return null;
	}

}
